package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.exception4project.CheckItemDeleteFailException;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.zip.DataFormatException;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public void add(CheckItem checkItem) {
        //System.out.println(checkItem.getCode());
        checkItemDao.add(checkItem);
    }

    /**
     * 模糊查询分页操作
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult get4Page(QueryPageBean queryPageBean) {
        Page<CheckItem> checkItems = PageHelper.startPage(queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize());
        if (queryPageBean.getQueryString() == null || "".equals(queryPageBean.getQueryString()))
            checkItemDao.getAll();
        else
            checkItemDao.getByLike(queryPageBean.getQueryString());
        System.out.println(checkItems.getTotal());
        return new PageResult(checkItems.getTotal(), checkItems);
    }

    @Override
    public void deleteCheckItem(Integer id) throws CheckItemDeleteFailException {
        //调用 dao 的删除操作
        long foreignKeyByCheckItemId = checkItemDao.getForeignKeyByCheckItemId(id);
        System.out.println(foreignKeyByCheckItemId);
        if (foreignKeyByCheckItemId > 0) {
            throw new CheckItemDeleteFailException();
        } else {
            checkItemDao.delete(id);
        }
    }

    @Override
    public void updateCheckItem(CheckItem checkItem) {
        checkItemDao.update(checkItem);
    }
}